package cz.muni.fi.rum.domainmodel;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author pmikulasek
 */
public class ReflectorListener extends ReflectorComponent implements Paramatizeabel{

    /**
     * Klic listener name hodnota pro tento klic muze byt napr."<code>listener/udp-0.0.0.0:5001</code>"
     */
    public static final String LISTENER_NAME_KEY = "Listener.name";
    /**
     * Klic listener type. Mozne hodnoty <code>udp/tcp/mcast</code> ktere jsou dostupne v enum: 
     * {@see ReflectorListenerType}
     */
    public static final String LISTENER_TYPE_KEY = "Listener.type";
    /**
     * Klic listener port. Hodnota obsahuje port tohoto listeneru.
     */
    public static final String LISTENER_PORT_KEY = "Listener.port";
    /**
     * Klic listener datasize. Hodnota obsahuje parametr datasize.
     */
    public static final String LISTENER_DATASIZE_KEY = "Listener.datasize";
    public static final String LISTENER_BIND_ADDRESS_KEY = "Listener.address";
    public static final String LISTENER_MULTICAST_GROUP_KEY = "Listener.group";
    public static final String LISTENER_DESTINATION_PORT_KEY = "Listener.destport";
    public static final String LISTENER_TTL_KEY = "Listener.ttl";
    private Map<String, String> params;

    public static enum ReflectorListenerType {

        udp, tcp, mcast
    }
    private Integer port;
    private Long dataSize;
    private String bindAddress;
    private String multicastGroup;
    private Integer destinationPort;
    private Short timeToLive;
    private ReflectorListenerType type;

    public ReflectorListener() {
        super();
        params = new HashMap<String, String>(8);
    }

    // <editor-fold defaultstate="collapsed" desc="setMethods">
    public void setBindAddress(String bindAddress) {
        this.bindAddress = bindAddress;
    }

    public void setDataSize(Long dataSize) {
        this.dataSize = dataSize;
    }

    public void setDestinationPort(Integer destinationPort) {
        this.destinationPort = destinationPort;
    }

    public void setMulticastGroup(String multicastGroup) {
        this.multicastGroup = multicastGroup;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public void setTimeToLive(Short timeToLive) {
        this.timeToLive = timeToLive;
    }

    public void setType(ReflectorListenerType type) {
        this.type = type;
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="getMethods">

    public String getBindAddress() {
        return bindAddress;
    }

    public Long getDataSize() {
        return dataSize;
    }

    public Integer getDestinationPort() {
        return destinationPort;
    }

    public String getMulticastGroup() {
        return multicastGroup;
    }

    public Integer getPort() {
        return port;
    }

    public Short getTimeToLive() {
        return timeToLive;
    }

    public ReflectorListenerType getType() {
        return type;
    }

    public String getName(){
        if(type == null | port == null){
            return null;
        }
        StringBuilder name = new StringBuilder("listener/");
        name.append(type);
        //TODO ==DomainModel== jmeno listeneru
        name.append("-0.0.0.0:");
        name.append(port);
        return name.toString();
    }
    // </editor-fold>

    /**
     * Metoda prida nebo odebere klic z mapy podle toho jestli je value null.
     * @param key
     * @param value
     */
    private void addToParams(String key, Object value) {
        if (value == null) {
            if (params.containsKey(key)) {
                params.remove(key);
            }
        } else {
            params.put(key, value.toString());
        }
    }

    /**
     * paramtery v mape pod temito klici jsou ulozeny informace o tomto Listeneru,
     * klic nemusi byt v mape obsazen pokud je paramter null
     * <p>
     * {@see #LISTENER_NAME_KEY} - oznaceni listeneru napr. ve tvaru "listener/udp-0.0.0.0:5001"
     * <br/>
     * {@see #LISTENER_TYPE_KEY} - typ [udp,tcp,mcast]
     * <br/>
     * {@see #LISTENER_PORT_KEY} - port listeneru
     * <br/>
     * {@see #LISTENER_BIND_ADDRESS_KEY},
     * {@see #LISTENER_DATASIZE_KEY},
     * {@see #LISTENER_DESTINATION_PORT_KEY},
     * {@see #LISTENER_MULTICAST_GROUP_KEY}
     * <br/>
     * {@see #LISTENER_TTL_KEY} - time to live
     * <br/>
     * </p>
     * @return
     */
    @Override
    public Map<String, String> getAllParams() {
        addToParams(LISTENER_NAME_KEY, getName());
        addToParams(LISTENER_TYPE_KEY, type);
        addToParams(LISTENER_PORT_KEY, port);
        addToParams(LISTENER_BIND_ADDRESS_KEY, bindAddress);
        addToParams(LISTENER_DATASIZE_KEY, dataSize);
        addToParams(LISTENER_DESTINATION_PORT_KEY, destinationPort);
        addToParams(LISTENER_MULTICAST_GROUP_KEY, multicastGroup);
        addToParams(LISTENER_TTL_KEY, timeToLive);
        return params;
    }
}
